package com.youi.web;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.common.DelStatus;
import com.youi.service.LeaderprofitService;
import com.youi.entity.Leaderprofit;
import com.youi.common.Common;
import com.youi.util.CommonInfoUtil;
import com.youi.util.DateUtil;
import com.youi.util.JsonUtil;
import com.youi.util.ObjectUtil;
import com.youi.util.Page;

/**
* 团长佣金信息控制层
* @author Administrator
*/
@Controller
public class LeaderprofitController  extends HttpRequestManage{

		@Autowired
		private LeaderprofitService leaderprofitService;

		/**
		 *添加或编辑团长佣金信息
		*/
		@ResponseBody
		@RequestMapping("addOrEditLeaderprofit")
		public String addOrEditLeaderprofit(ModelMap modelMap,HttpServletRequest request)throws BusinessException,ParseException{
			Long id=optLong("id",request);
			Leaderprofit leaderprofit=null;
			if (id!=null) {
				leaderprofit=leaderprofitService.get(id);
			}else {
				leaderprofit=new Leaderprofit();
				leaderprofit.setDelStatus(DelStatus.NODEL);
				leaderprofit.setGmtCreated(new Date());
			}
			Long leaderId=optLong("leaderId",request);
			if (leaderId!=null){
				leaderprofit.setLeaderId(leaderId);
			}
			Double totalAmount=optDouble("totalAmount",request);
			if (totalAmount!=null){
				leaderprofit.setTotalAmount(totalAmount);
			}
			Double withdres=optDouble("withdres",request);
			if (withdres!=null){
				leaderprofit.setWithdres(withdres);
			}
			Double frozen=optDouble("frozen",request);
			if (frozen!=null){
				leaderprofit.setFrozen(frozen);
			}
			Double balance=optDouble("balance",request);
			if (balance!=null){
				leaderprofit.setBalance(balance);
			}
			String companyCode=optString("companyCode",request);
			if (companyCode!=null){
				leaderprofit.setCompanyCode(companyCode);
			}
			leaderprofit.setGmtModify(new Date());
			leaderprofitService.saveOrUpdate(leaderprofit);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 *批量删除团长佣金信息
		*/
		@ResponseBody
		@RequestMapping("delLeaderprofitByIds")
		public String delLeaderprofitByIds(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			String ids=getString("ids",request);
			List<Long> idsList=ObjectUtil.getIdListByIds(ids);
			if (idsList==null||idsList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "ids参数错误");
			}
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put(Common.ID, idsList);
			List<Leaderprofit> leaderprofitList= leaderprofitService.getByParams(params);
			if (leaderprofitList==null||leaderprofitList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			for (Leaderprofit leaderprofit : leaderprofitList) {
				leaderprofit.setDelStatus(DelStatus.DEL);
			}
			leaderprofitService.saveOrUpdateAll(leaderprofitList);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 * 分页查询团长佣金信息
		*/
		@ResponseBody
		@RequestMapping("getLeaderprofit")
		public String getLeaderprofit(ModelMap modelMap,HttpServletRequest request) throws BusinessException,ParseException{
			Integer pageIndex=optInteger("pageIndex",request,1);
			Integer pageNum=optInteger("pageNum",request,10);
			String des=optString("des",request,Common.DESC);
			String pms=optString("pms",request);
			Date start=optDate("start",request,"yyyy-MM-dd HH:mm:ss");
			Date end=optDate("end",request,"yyyy-MM-dd HH:mm:ss");
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			Map<String, Object> sortPram=new HashMap<String, Object>();
			sortPram.put(Common.ID,des);
			Map<String, Object> searchPram=null;
			if (pms!=null) {
				searchPram=new HashMap<String, Object>();
			}
			Map<String, Object> startTime=null;
			if(start!=null){
				startTime=new HashMap<String, Object>();
				startTime.put(Common.GMTCREATED,start);
			}
			Map<String, Object> endTime=null;
			if(end!=null){
				endTime=new HashMap<String, Object>();
				endTime.put(Common.GMTCREATED,end);
			}
			List<Leaderprofit> leaderprofitList=leaderprofitService.getByParamsList(prams, searchPram,sortPram,  (pageIndex-1)*pageNum, pageNum, startTime, endTime);
			if (leaderprofitList==null||leaderprofitList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			Integer pageCount=leaderprofitService.getCountByParamss(prams, searchPram, startTime, endTime);
			modelMap.put(CommonInfoUtil.PAGECOUNT, Page.getCount(pageCount, pageNum));//pageCount  总页数
			modelMap.put(CommonInfoUtil.PAGEINDEX, pageIndex);			//pageIndex     查询页数
			modelMap.put(CommonInfoUtil.ALLCOUINT,pageCount);
			modelMap.put(CommonInfoUtil.DATA, leaderprofitList);		//jsonObjectList json对象集合
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);
		}

		/**
		 *根据id查询团长佣金信息
		*/
		@ResponseBody
		@RequestMapping("getLeaderprofitById")
		public String getLeaderprofitById(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			Long id=getLong("id",request);
			Leaderprofit leaderprofit=leaderprofitService.get(id);
			if (leaderprofit==null) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",leaderprofit);//jsonObject	json对象
		}

		/**
		 * 查询所有团长佣金信息
		 * @param modelMap
		 * @param session
		 * @return
		 */
		@ResponseBody
		@RequestMapping("getAllLeaderprofit")
		public String getAllLeaderprofit(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			List<Leaderprofit> leaderprofitList= leaderprofitService.getByParams(null);
			if (leaderprofitList==null||leaderprofitList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",leaderprofitList);//jsonObjectList	json对象集合
		}
}
